package com.atguigu.yygh.hosp.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin  //允许跨域访问
public class HospitalSetController {

	@Autowired    //注入 service
	private HospitalSetService hospitalSetService;
	
	//1.查询医院设置表里的所有信息
	@ApiOperation(value = "获取所有医院设置")
	@GetMapping("/findAll")
	public Result findAllHospitalSet(){
		//  mp封装    调用  service 的list方法
		List<HospitalSet> list = hospitalSetService.list();
		Result<List<HospitalSet>> result = Result.ok(list);
		return result;
	}
	
	//2.删除医院设置
	@ApiOperation(value = "逻辑删除医院设置")
	@DeleteMapping("{id}")
	public Result removeHospSet(@PathVariable(value = "id") Long id){
		boolean flag = hospitalSetService.removeById(id);
		if(flag){
			return Result.ok();
		}else{
			return Result.fail();
		}
	}
	
	//3.条件查询  带分页
	@ApiOperation(value = "条件查询 带分页")
	@PostMapping("/findPage/{current}/{limit}")
	public Result findPageHospSet(@PathVariable(value = "current") Integer current,
			                      @PathVariable(value = "limit") Integer limit,
			                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
		//@RequestBody(required = false)  通过 json数据传参数  false 表示值可以为空   更改请求方式为PostMapping  前端传值方便
		// 先创建 Page 对象 传递当前页  每页记录数
		Page<HospitalSet> page = new Page<HospitalSet>(current,limit);
		//构建条件
		QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
		String hostname = hospitalSetQueryVo.getHostname(); //医院名称
		String hostcode = hospitalSetQueryVo.getHostcode(); //医院编号
		if(!StringUtils.isEmpty(hostname)){
		    queryWrapper.like("hostname", hospitalSetQueryVo.getHostname());
		}
		if(!StringUtils.isEmpty(hostcode)){
		    queryWrapper.eq("hostcode", hospitalSetQueryVo.getHostcode());
		}
		//调用方法实现分页查询
		Page<HospitalSet> pages = hospitalSetService.page(page, queryWrapper);
		
		//返回结果
		return Result.ok(pages);
	}
	
	//4.添加医院设置接口
	@ApiOperation(value = "添加医院设置接口")
	@PostMapping("/saveHosp")
	public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
		//设置 status 状态  1 使用  0 不能使用
		hospitalSet.setStatus(1);
		Random random  = new Random();
		hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
		
		//调用service
		boolean insert = hospitalSetService.save(hospitalSet);
		if(insert){
		    return Result.ok();
		}else{
			return Result.fail();
		}
	}
	
	//5.根据 id 先获取医院设置  
	@ApiOperation(value = "根据id 获取医院设置")
	@GetMapping("/getHospSet/{id}")
	public Result getHospSet(@PathVariable(value = "id")Integer id){
		// 手动 模拟异常
//		try {
//			int a = 1/0;   // 报  500 错误
//		}catch(Exception e){
//			//手动 抛出自定义异常
//			throw new YyghException("失败",201);
//		}
		HospitalSet hospitalSet = hospitalSetService.getById(id);
		return Result.ok(hospitalSet);
	}
	
	//6.修改医院设置
	@ApiOperation(value = "修改医院设置")
	@PostMapping("/updateHospSet")
	public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
		
		boolean update = hospitalSetService.updateById(hospitalSet);
		if(update){
		    return Result.ok();
		}else{
			return Result.fail();
		}
	}
	
	//7.批量删除医院设置
	@ApiOperation(value = "批量删除医院设置")
	@DeleteMapping("/batchRemove")
	public Result batchRemoveHospSet(@RequestBody List<Integer> idList){
		
		boolean flag = hospitalSetService.removeByIds(idList);
		if(flag){
			return Result.ok();
		}else{
			return Result.fail();
		}
	}
	
	//8.医院设置 锁定和解锁  status 
	//医院设置是 解锁状态，才可进行与医院系统对接，进行数据操作，锁定，则不能进行对接
	@ApiOperation(value = "医院设置 锁定和解锁")
	@PutMapping("/lock/{id}/{status}")
	public Result lock(@PathVariable(value = "id") Integer id,@PathVariable(value = "status") Integer status){
		//根据 id 查询出 医院设置的信息   然后修改 status 的值  根据 0 1 来判断是否锁定  解锁  然后更新到数据库
		HospitalSet hospitalSet = hospitalSetService.getById(id);
		//设置状态
		hospitalSet.setStatus(status);
		//调用方法  更新到数据库
		hospitalSetService.updateById(hospitalSet);
		return Result.ok();
	}
	
	//9.发送签名密钥  
	//医院接口和平台进行对接，双方约定好密钥  添加时生成了密钥，将密钥发送给医院接口，医院对该密钥进行响应的处理，才能实现相互的对接
	//发送短信验证码进行操作   该示例为简单实现   后续修改为短信验证码
	@ApiOperation(value = "发送签名密钥")
	@PutMapping("/sendKey/{id}")
	public Result sendKey(@PathVariable(value = "id") Integer id){
		HospitalSet hospitalSet = hospitalSetService.getById(id);
		String signKey = hospitalSet.getSignKey();
		String hostCode = hospitalSet.getHostcode();
		// 发送短信
		return Result.ok();
	}
}
